<template>
  <div class="member-oder">
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
        />
      </XtxTabs>
      <!-- 订单列表 -->
      <div class="order-list">
        <div v-if="loading" class="loading" />
        <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
        <OrderItem
          @on-cancel="handlerCancel"
          @on-delete="handlerDelete"
          @on-confirm="handlerConfirm"
          @on-logistics="handlerLogistics"
          v-for="item in orderList"
          :key="item.id"
          :order="item"
        />
      </div>
      <!-- 分页组件 -->
      <XtxPagination
      v-if="total > 0"
      @current-change="reqParams.page=$event"
      :total="total"
      :pageSize="reqParams.pageSize"
      :currentPage="reqParams.page" />
      <!-- 取消原有组件 -->
      <OrderCancel ref="orderCancelCom"/>
      <!-- 查看物流 -->
      <OrderLogistics ref="logisticsOrderCom" />
  </div>
</template>

<script>
import { defineAsyncComponent, reactive, ref, watch } from 'vue'
import { deleteOrder, findOrderList, confirmOrder } from '@/api/order'
import { orderStatus } from '@/api/constants'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
export default {
  name: 'MemberOder',
  components: {
    OrderItem: defineAsyncComponent(() => import('./components/order-item.vue')),
    OrderCancel: defineAsyncComponent(() => import('./components/order-cancel.vue')),
    OrderLogistics: defineAsyncComponent(() => import('./components/order-logistics.vue'))
  },
  setup () {
    // const activeName = ref('first')
    // const activeName = ref('name1')
    const activeName = ref('all')
    // 筛选条件
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })

    const orderList = ref([])
    const loading = ref(false)
    const total = ref(0)

    const getOrderList = () => {
      findOrderList(reqParams).then(data => {
        loading.value = true
        orderList.value = data.result.items
        total.value = data.result.counts
        setTimeout(() => {
          loading.value = false
        }, 200)
      })
    }
    // 筛选条件变化重新加载
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })

    // 点击选项卡
    const tabClick = ({ index }) => {
      console.log(index)
      // 此时：tab.index 就是订单的状态
      reqParams.page = 1
      reqParams.orderState = index
    }
    const handlerDelete = (item) => {
      Confirm({ text: '亲。您确认删除该条订单吗？' }).then(() => {
        deleteOrder(item.id).then(() => {
          Message({ type: 'success', text: '删除订单成功' })
          getOrderList()
        })
      }).catch(e => {})
    }
    return {
      activeName,
      tabClick,
      orderStatus,
      orderList,
      reqParams,
      total,
      loading,
      handlerDelete,
      ...useCancel(),
      ...useConfirm(),
      ...useLogistics()
    }
  }
}
// 取消订单逻辑
export const useCancel = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  // 点击取消
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}
// 确认收货
export const useConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ text: '确认收货成功', type: 'success' })
        // 确认收货-->待评价
        order.orderState = 4
      })
    })
  }
  return { handlerConfirm }
}
// 查看物流
export const useLogistics = () => {
  const logisticsOrderCom = ref(null)
  const handlerLogistics = (order) => {
    logisticsOrderCom.value.open(order)
  }
  return { handlerLogistics, logisticsOrderCom }
}
</script>

<style lang="less" scoped>
.member-oder{
  height: 100%;
  background-color: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
}
.loading {
  height: 100%;
  width: 100%;
  max-height: 400px;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url('~@/assets/images/loading.gif') no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>

<!--<template>
  <div class="member-oder">
    <!~~ <RouterLink to="/member/order/1001" >订单1</RouterLink>
    <RouterLink to="/member/order/1002" >订单2</RouterLink> ~~>
    <!~~ 体验jsx语法 ~~>
    <!~~
      <XtxTabs>
        <XtxTabsPanel label="选项卡1" name="first">内容1</XtxTabsPanel>
        <XtxTabsPanel label="选项卡2" name="second">内容2</XtxTabsPanel>
      </XtxTabs>
    ~~>
    <!~~ <XtxTabs v-model="activeName" @tab-click="changeTab">
      <XtxTabsPanel label="选项卡0" name="first">内容0</XtxTabsPanel>
      <XtxTabsPanel v-for="i in 4" :key="i"  :label="`选项卡${i}`" :name="`name${i}`" >内容{{ i }}</XtxTabsPanel>
    </XtxTabs> -->
    <!-- <XtxTabs v-model="activeName">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
        >{{item.label}}</XtxTabsPanel
      >
    </XtxTabs> ~~>
  </div>
</template>-->
